/*
 * OpenThe9Box (OT9B): Java(TM) API for NeufBox REST interface.
 * Copyright (C) 2015 ScalAgent D.T.
 * Copyright (C) 2015 Turbotel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA.
 *
 * Initial developer(s): Freyssinet Andre (ScalAgent D.T.)
 * Contributor(s): 
 */
package org.turbotel.ot9b.tools;

/**
 * Simple class for logging.
 */
public class Log {
  public final static boolean debug = true;
  
  /**
   * Get simple class name from full class name
   * @param fullClassName
   * @return
   */
  private static String getSimpleName(String fullClassName) {
    int index = fullClassName.lastIndexOf('.');
    return fullClassName.substring(index+1, fullClassName.length());
  }

  /**
   * Display debug messages
   * @param message
   */
  public static void d(String message) {
    Throwable stack = new Throwable().fillInStackTrace();
    StackTraceElement[] trace = stack.getStackTrace();

    String fullClassName = trace[1].getClassName();
    String simpleName = getSimpleName(fullClassName);

    System.out.println(simpleName + "." + trace[1].getMethodName() + " - li:" + trace[1].getLineNumber() + " - " + message);
  }

  /**
   * Display error messages
   * @param message
   * @param e
   */
  public static void e(String message, Throwable e) {
    Throwable stack = new Throwable().fillInStackTrace();
    StackTraceElement[] trace = stack.getStackTrace();

    String fullClassName = trace[1].getClassName();
    String simpleName = getSimpleName(fullClassName);

    System.err.println(simpleName + "." + trace[1].getMethodName() + " - li:" + trace[1].getLineNumber() + " - " + message);
    System.err.println(simpleName + "." + trace[1].getMethodName() + " - li:" + e.getMessage());
    e.printStackTrace();
  }

  /**
   * Display error messages
   * @param message
   */
  public static void e(String message) {
    Throwable stack = new Throwable().fillInStackTrace();
    StackTraceElement[] trace = stack.getStackTrace();

    String fullClassName = trace[1].getClassName();
    String simpleName = getSimpleName(fullClassName);

    System.err.println(simpleName + "." + trace[1].getMethodName() + " - li:" + trace[1].getLineNumber() + " - " + message);
  }

}